<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
class PHPExcel_Writer_Excel5_Workbook extends PHPExcel_Writer_Excel5_BIFFwriter
{
    private $parser;
    private $biffSize;
    private $xfWriters = [];
    private $palette;
    private $codepage;
    private $countryCode;
    private $phpExcel;
    private $fontWriters = [];
    private $addedFonts = [];
    private $numberFormats = [];
    private $addedNumberFormats = [];
    private $worksheetSizes = [];
    private $worksheetOffsets = [];
    private $stringTotal;
    private $stringUnique;
    private $stringTable;
    private $colors;
    private $escher;
    public function __construct(PHPExcel $phpExcel = NULL, &$str_total, &$str_unique, &$str_table, &$colors, $parser)
    {
        parent::__construct();
        $this->parser = $parser;
        $this->biffSize = 0;
        $this->palette = [];
        $this->countryCode = -1;
        $this->stringTotal =& $str_total;
        $this->stringUnique =& $str_unique;
        $this->stringTable =& $str_table;
        $this->colors =& $colors;
        $this->setPaletteXl97();
        $this->phpExcel = $phpExcel;
        $this->codepage = 1200;
        $countSheets = $phpExcel->getSheetCount();
        for ($i = 0; $i < $countSheets; $i++) {
            $phpSheet = $phpExcel->getSheet($i);
            $this->parser->setExtSheet($phpSheet->getTitle(), $i);
            $supbook_index = 0;
            $ref = pack("vvv", $supbook_index, $i, $i);
            $this->parser->references[] = $ref;
            if($phpSheet->isTabColorSet()) {
                $this->addColor($phpSheet->getTabColor()->getRGB());
            }
        }
    }
    public function addXfWriter($style, $isStyleXf = false)
    {
        $xfWriter = new PHPExcel_Writer_Excel5_Xf($style);
        $xfWriter->setIsStyleXf($isStyleXf);
        $fontIndex = $this->addFont($style->getFont());
        $xfWriter->setFontIndex($fontIndex);
        $xfWriter->setFgColor($this->addColor($style->getFill()->getStartColor()->getRGB()));
        $xfWriter->setBgColor($this->addColor($style->getFill()->getEndColor()->getRGB()));
        $xfWriter->setBottomColor($this->addColor($style->getBorders()->getBottom()->getColor()->getRGB()));
        $xfWriter->setTopColor($this->addColor($style->getBorders()->getTop()->getColor()->getRGB()));
        $xfWriter->setRightColor($this->addColor($style->getBorders()->getRight()->getColor()->getRGB()));
        $xfWriter->setLeftColor($this->addColor($style->getBorders()->getLeft()->getColor()->getRGB()));
        $xfWriter->setDiagColor($this->addColor($style->getBorders()->getDiagonal()->getColor()->getRGB()));
        if($style->getNumberFormat()->getBuiltInFormatCode() === false) {
            $numberFormatHashCode = $style->getNumberFormat()->getHashCode();
            if(isset($this->addedNumberFormats[$numberFormatHashCode])) {
                $numberFormatIndex = $this->addedNumberFormats[$numberFormatHashCode];
            } else {
                $numberFormatIndex = 164 + count($this->numberFormats);
                $this->numberFormats[$numberFormatIndex] = $style->getNumberFormat();
                $this->addedNumberFormats[$numberFormatHashCode] = $numberFormatIndex;
            }
        } else {
            $numberFormatIndex = (int) $style->getNumberFormat()->getBuiltInFormatCode();
        }
        $xfWriter->setNumberFormatIndex($numberFormatIndex);
        $this->xfWriters[] = $xfWriter;
        $xfIndex = count($this->xfWriters) - 1;
        return $xfIndex;
    }
    public function addFont(PHPExcel_Style_Font $font)
    {
        $fontHashCode = $font->getHashCode();
        if(isset($this->addedFonts[$fontHashCode])) {
            $fontIndex = $this->addedFonts[$fontHashCode];
        } else {
            $countFonts = count($this->fontWriters);
            $fontIndex = $countFonts < 4 ? $countFonts : $countFonts + 1;
            $fontWriter = new PHPExcel_Writer_Excel5_Font($font);
            $fontWriter->setColorIndex($this->addColor($font->getColor()->getRGB()));
            $this->fontWriters[] = $fontWriter;
            $this->addedFonts[$fontHashCode] = $fontIndex;
        }
        return $fontIndex;
    }
    private function addColor($rgb)
    {
        if(!isset($this->colors[$rgb])) {
            if(count($this->colors) < 57) {
                $colorIndex = 8 + count($this->colors);
                $this->palette[$colorIndex] = [hexdec(substr($rgb, 0, 2)), hexdec(substr($rgb, 2, 2)), hexdec(substr($rgb, 4)), 0];
                $this->colors[$rgb] = $colorIndex;
            } else {
                $colorIndex = 0;
            }
        } else {
            $colorIndex = $this->colors[$rgb];
        }
        return $colorIndex;
    }
    private function setPaletteXl97()
    {
        $this->palette = ["8" => [0, 0, 0, 0], "9" => [255, 255, 255, 0], "10" => [255, 0, 0, 0], "11" => [0, 255, 0, 0], "12" => [0, 0, 255, 0], "13" => [255, 255, 0, 0], "14" => [255, 0, 255, 0], "15" => [0, 255, 255, 0], "16" => [128, 0, 0, 0], "17" => [0, 128, 0, 0], "18" => [0, 0, 128, 0], "19" => [128, 128, 0, 0], "20" => [128, 0, 128, 0], "21" => [0, 128, 128, 0], "22" => [192, 192, 192, 0], "23" => [128, 128, 128, 0], "24" => [153, 153, 255, 0], "25" => [153, 51, 102, 0], "26" => [255, 255, 204, 0], "27" => [204, 255, 255, 0], "28" => [102, 0, 102, 0], "29" => [255, 128, 128, 0], "30" => [0, 102, 204, 0], "31" => [204, 204, 255, 0], "32" => [0, 0, 128, 0], "33" => [255, 0, 255, 0], "34" => [255, 255, 0, 0], "35" => [0, 255, 255, 0], "36" => [128, 0, 128, 0], "37" => [128, 0, 0, 0], "38" => [0, 128, 128, 0], "39" => [0, 0, 255, 0], "40" => [0, 204, 255, 0], "41" => [204, 255, 255, 0], "42" => [204, 255, 204, 0], "43" => [255, 255, 153, 0], "44" => [153, 204, 255, 0], "45" => [255, 153, 204, 0], "46" => [204, 153, 255, 0], "47" => [255, 204, 153, 0], "48" => [51, 102, 255, 0], "49" => [51, 204, 204, 0], "50" => [153, 204, 0, 0], "51" => [255, 204, 0, 0], "52" => [255, 153, 0, 0], "53" => [255, 102, 0, 0], "54" => [102, 102, 153, 0], "55" => [150, 150, 150, 0], "56" => [0, 51, 102, 0], "57" => [51, 153, 102, 0], "58" => [0, 51, 0, 0], "59" => [51, 51, 0, 0], "60" => [153, 51, 0, 0], "61" => [153, 51, 102, 0], "62" => [51, 51, 153, 0], "63" => [51, 51, 51, 0]];
    }
    public function writeWorkbook($pWorksheetSizes = NULL)
    {
        $this->worksheetSizes = $pWorksheetSizes;
        $total_worksheets = $this->phpExcel->getSheetCount();
        $this->storeBof(5);
        $this->writeCodepage();
        $this->writeWindow1();
        $this->writeDateMode();
        $this->writeAllFonts();
        $this->writeAllNumberFormats();
        $this->writeAllXfs();
        $this->writeAllStyles();
        $this->writePalette();
        $part3 = "";
        if($this->countryCode != -1) {
            $part3 .= $this->writeCountry();
        }
        $part3 .= $this->writeRecalcId();
        $part3 .= $this->writeSupbookInternal();
        $part3 .= $this->writeExternalsheetBiff8();
        $part3 .= $this->writeAllDefinedNamesBiff8();
        $part3 .= $this->writeMsoDrawingGroup();
        $part3 .= $this->writeSharedStringsTable();
        $part3 .= $this->writeEof();
        $this->calcSheetOffsets();
        for ($i = 0; $i < $total_worksheets; $i++) {
            $this->writeBoundSheet($this->phpExcel->getSheet($i), $this->worksheetOffsets[$i]);
        }
        $this->_data .= $part3;
        return $this->_data;
    }
    private function calcSheetOffsets()
    {
        $boundsheet_length = 10;
        $offset = $this->_datasize;
        $total_worksheets = count($this->phpExcel->getAllSheets());
        foreach ($this->phpExcel->getWorksheetIterator() as $sheet) {
            $offset += $boundsheet_length + strlen(PHPExcel_Shared_String::UTF8toBIFF8UnicodeShort($sheet->getTitle()));
        }
        for ($i = 0; $i < $total_worksheets; $i++) {
            $this->worksheetOffsets[$i] = $offset;
            $offset += $this->worksheetSizes[$i];
        }
        $this->biffSize = $offset;
    }
    private function writeAllFonts()
    {
        foreach ($this->fontWriters as $fontWriter) {
            $this->append($fontWriter->writeFont());
        }
    }
    private function writeAllNumberFormats()
    {
        foreach ($this->numberFormats as $numberFormatIndex => $numberFormat) {
            $this->writeNumberFormat($numberFormat->getFormatCode(), $numberFormatIndex);
        }
    }
    private function writeAllXfs()
    {
        foreach ($this->xfWriters as $xfWriter) {
            $this->append($xfWriter->writeXf());
        }
    }
    private function writeAllStyles()
    {
        $this->writeStyle();
    }
    private function writeExternals()
    {
        $countSheets = $this->phpExcel->getSheetCount();
        $this->writeExternalCount($countSheets);
        for ($i = 0; $i < $countSheets; $i++) {
            $this->writeExternalSheet($this->phpExcel->getSheet($i)->getTitle());
        }
    }
    private function writeNames()
    {
        $total_worksheets = $this->phpExcel->getSheetCount();
        for ($i = 0; $i < $total_worksheets; $i++) {
            $sheetSetup = $this->phpExcel->getSheet($i)->getPageSetup();
            if($sheetSetup->isPrintAreaSet()) {
                $printArea = PHPExcel_Cell::splitRange($sheetSetup->getPrintArea());
                $printArea = $printArea[0];
                $printArea[0] = PHPExcel_Cell::coordinateFromString($printArea[0]);
                $printArea[1] = PHPExcel_Cell::coordinateFromString($printArea[1]);
                $print_rowmin = $printArea[0][1] - 1;
                $print_rowmax = $printArea[1][1] - 1;
                $print_colmin = PHPExcel_Cell::columnIndexFromString($printArea[0][0]) - 1;
                $print_colmax = PHPExcel_Cell::columnIndexFromString($printArea[1][0]) - 1;
                $this->writeNameShort($i, 6, $print_rowmin, $print_rowmax, $print_colmin, $print_colmax);
            }
        }
        for ($i = 0; $i < $total_worksheets; $i++) {
            $sheetSetup = $this->phpExcel->getSheet($i)->getPageSetup();
            if($sheetSetup->isColumnsToRepeatAtLeftSet() && $sheetSetup->isRowsToRepeatAtTopSet()) {
                $repeat = $sheetSetup->getColumnsToRepeatAtLeft();
                $colmin = PHPExcel_Cell::columnIndexFromString($repeat[0]) - 1;
                $colmax = PHPExcel_Cell::columnIndexFromString($repeat[1]) - 1;
                $repeat = $sheetSetup->getRowsToRepeatAtTop();
                $rowmin = $repeat[0] - 1;
                $rowmax = $repeat[1] - 1;
                $this->writeNameLong($i, 7, $rowmin, $rowmax, $colmin, $colmax);
            } elseif($sheetSetup->isColumnsToRepeatAtLeftSet() || $sheetSetup->isRowsToRepeatAtTopSet()) {
                if($sheetSetup->isColumnsToRepeatAtLeftSet()) {
                    $repeat = $sheetSetup->getColumnsToRepeatAtLeft();
                    $colmin = PHPExcel_Cell::columnIndexFromString($repeat[0]) - 1;
                    $colmax = PHPExcel_Cell::columnIndexFromString($repeat[1]) - 1;
                } else {
                    $colmin = 0;
                    $colmax = 255;
                }
                if($sheetSetup->isRowsToRepeatAtTopSet()) {
                    $repeat = $sheetSetup->getRowsToRepeatAtTop();
                    $rowmin = $repeat[0] - 1;
                    $rowmax = $repeat[1] - 1;
                } else {
                    $rowmin = 0;
                    $rowmax = 65535;
                }
                $this->writeNameShort($i, 7, $rowmin, $rowmax, $colmin, $colmax);
            }
        }
    }
    private function writeAllDefinedNamesBiff8()
    {
        $chunk = "";
        if(0 < count($this->phpExcel->getNamedRanges())) {
            $namedRanges = $this->phpExcel->getNamedRanges();
            foreach ($namedRanges as $namedRange) {
                $range = PHPExcel_Cell::splitRange($namedRange->getRange());
                for ($i = 0; $i < count($range); $i++) {
                    $range[$i][0] = "'" . str_replace("'", "''", $namedRange->getWorksheet()->getTitle()) . "'!" . PHPExcel_Cell::absoluteCoordinate($range[$i][0]);
                    if(isset($range[$i][1])) {
                        $range[$i][1] = PHPExcel_Cell::absoluteCoordinate($range[$i][1]);
                    }
                }
                $range = PHPExcel_Cell::buildRange($range);
                try {
                    $error = $this->parser->parse($range);
                    $formulaData = $this->parser->toReversePolish();
                    if(isset($formulaData[0]) && ($formulaData[0] == "z" || $formulaData[0] == "Z")) {
                        $formulaData = ":" . substr($formulaData, 1);
                    }
                    if($namedRange->getLocalOnly()) {
                        $scope = $this->phpExcel->getIndex($namedRange->getScope()) + 1;
                    } else {
                        $scope = 0;
                    }
                    $chunk .= $this->writeData($this->writeDefinedNameBiff8($namedRange->getName(), $formulaData, $scope, false));
                } catch (PHPExcel_Exception $e) {
                }
            }
        }
        $total_worksheets = $this->phpExcel->getSheetCount();
        for ($i = 0; $i < $total_worksheets; $i++) {
            $sheetSetup = $this->phpExcel->getSheet($i)->getPageSetup();
            if($sheetSetup->isColumnsToRepeatAtLeftSet() && $sheetSetup->isRowsToRepeatAtTopSet()) {
                $repeat = $sheetSetup->getColumnsToRepeatAtLeft();
                $colmin = PHPExcel_Cell::columnIndexFromString($repeat[0]) - 1;
                $colmax = PHPExcel_Cell::columnIndexFromString($repeat[1]) - 1;
                $repeat = $sheetSetup->getRowsToRepeatAtTop();
                $rowmin = $repeat[0] - 1;
                $rowmax = $repeat[1] - 1;
                $formulaData = pack("Cv", 41, 23);
                $formulaData .= pack("Cvvvvv", 59, $i, 0, 65535, $colmin, $colmax);
                $formulaData .= pack("Cvvvvv", 59, $i, $rowmin, $rowmax, 0, 255);
                $formulaData .= pack("C", 16);
                $chunk .= $this->writeData($this->writeDefinedNameBiff8(pack("C", 7), $formulaData, $i + 1, true));
            } elseif($sheetSetup->isColumnsToRepeatAtLeftSet() || $sheetSetup->isRowsToRepeatAtTopSet()) {
                if($sheetSetup->isColumnsToRepeatAtLeftSet()) {
                    $repeat = $sheetSetup->getColumnsToRepeatAtLeft();
                    $colmin = PHPExcel_Cell::columnIndexFromString($repeat[0]) - 1;
                    $colmax = PHPExcel_Cell::columnIndexFromString($repeat[1]) - 1;
                } else {
                    $colmin = 0;
                    $colmax = 255;
                }
                if($sheetSetup->isRowsToRepeatAtTopSet()) {
                    $repeat = $sheetSetup->getRowsToRepeatAtTop();
                    $rowmin = $repeat[0] - 1;
                    $rowmax = $repeat[1] - 1;
                } else {
                    $rowmin = 0;
                    $rowmax = 65535;
                }
                $formulaData = pack("Cvvvvv", 59, $i, $rowmin, $rowmax, $colmin, $colmax);
                $chunk .= $this->writeData($this->writeDefinedNameBiff8(pack("C", 7), $formulaData, $i + 1, true));
            }
        }
        for ($i = 0; $i < $total_worksheets; $i++) {
            $sheetSetup = $this->phpExcel->getSheet($i)->getPageSetup();
            if($sheetSetup->isPrintAreaSet()) {
                $printArea = PHPExcel_Cell::splitRange($sheetSetup->getPrintArea());
                $countPrintArea = count($printArea);
                $formulaData = "";
                for ($j = 0; $j < $countPrintArea; $j++) {
                    $printAreaRect = $printArea[$j];
                    $printAreaRect[0] = PHPExcel_Cell::coordinateFromString($printAreaRect[0]);
                    $printAreaRect[1] = PHPExcel_Cell::coordinateFromString($printAreaRect[1]);
                    $print_rowmin = $printAreaRect[0][1] - 1;
                    $print_rowmax = $printAreaRect[1][1] - 1;
                    $print_colmin = PHPExcel_Cell::columnIndexFromString($printAreaRect[0][0]) - 1;
                    $print_colmax = PHPExcel_Cell::columnIndexFromString($printAreaRect[1][0]) - 1;
                    $formulaData .= pack("Cvvvvv", 59, $i, $print_rowmin, $print_rowmax, $print_colmin, $print_colmax);
                    if(0 < $j) {
                        $formulaData .= pack("C", 16);
                    }
                }
                $chunk .= $this->writeData($this->writeDefinedNameBiff8(pack("C", 6), $formulaData, $i + 1, true));
            }
        }
        for ($i = 0; $i < $total_worksheets; $i++) {
            $sheetAutoFilter = $this->phpExcel->getSheet($i)->getAutoFilter();
            $autoFilterRange = $sheetAutoFilter->getRange();
            if(!empty($autoFilterRange)) {
                $rangeBounds = PHPExcel_Cell::rangeBoundaries($autoFilterRange);
                $name = pack("C", 13);
                $chunk .= $this->writeData($this->writeShortNameBiff8($name, $i + 1, $rangeBounds, true));
            }
        }
        return $chunk;
    }
    private function writeDefinedNameBiff8($name, $formulaData, $sheetIndex = 0, $isBuiltIn = false)
    {
        $record = 24;
        $options = $isBuiltIn ? 32 : 0;
        $nlen = PHPExcel_Shared_String::CountCharacters($name);
        $name = substr(PHPExcel_Shared_String::UTF8toBIFF8UnicodeLong($name), 2);
        $sz = strlen($formulaData);
        $data = pack("vCCvvvCCCC", $options, 0, $nlen, $sz, 0, $sheetIndex, 0, 0, 0, 0) . $name . $formulaData;
        $length = strlen($data);
        $header = pack("vv", $record, $length);
        return $header . $data;
    }
    private function writeShortNameBiff8($name, $sheetIndex = 0, $rangeBounds, $isHidden = false)
    {
        $record = 24;
        $options = $isHidden ? 33 : 0;
        $extra = pack("Cvvvvv", 59, $sheetIndex - 1, $rangeBounds[0][1] - 1, $rangeBounds[1][1] - 1, $rangeBounds[0][0] - 1, $rangeBounds[1][0] - 1);
        $sz = strlen($extra);
        $data = pack("vCCvvvCCCCC", $options, 0, 1, $sz, 0, $sheetIndex, 0, 0, 0, 0, 0) . $name . $extra;
        $length = strlen($data);
        $header = pack("vv", $record, $length);
        return $header . $data;
    }
    private function writeCodepage()
    {
        $record = 66;
        $length = 2;
        $cv = $this->codepage;
        $header = pack("vv", $record, $length);
        $data = pack("v", $cv);
        $this->append($header . $data);
    }
    private function writeWindow1()
    {
        $record = 61;
        $length = 18;
        $xWn = 0;
        $yWn = 0;
        $dxWn = 9660;
        $dyWn = 5490;
        $grbit = 56;
        $ctabsel = 1;
        $wTabRatio = 600;
        $itabFirst = 0;
        $itabCur = $this->phpExcel->getActiveSheetIndex();
        $header = pack("vv", $record, $length);
        $data = pack("vvvvvvvvv", $xWn, $yWn, $dxWn, $dyWn, $grbit, $itabCur, $itabFirst, $ctabsel, $wTabRatio);
        $this->append($header . $data);
    }
    private function writeBoundSheet($sheet, $offset)
    {
        $sheetname = $sheet->getTitle();
        $record = 133;
        $sheet->getSheetState();
        switch ($sheet->getSheetState()) {
            case PHPExcel_Worksheet::SHEETSTATE_VISIBLE:
                $ss = 0;
                break;
            case PHPExcel_Worksheet::SHEETSTATE_HIDDEN:
                $ss = 1;
                break;
            case PHPExcel_Worksheet::SHEETSTATE_VERYHIDDEN:
                $ss = 2;
                break;
            default:
                $ss = 0;
                $st = 0;
                $grbit = 0;
                $data = pack("VCC", $offset, $ss, $st);
                $data .= PHPExcel_Shared_String::UTF8toBIFF8UnicodeShort($sheetname);
                $length = strlen($data);
                $header = pack("vv", $record, $length);
                $this->append($header . $data);
        }
    }
    private function writeSupbookInternal()
    {
        $record = 430;
        $length = 4;
        $header = pack("vv", $record, $length);
        $data = pack("vv", $this->phpExcel->getSheetCount(), 1025);
        return $this->writeData($header . $data);
    }
    private function writeExternalsheetBiff8()
    {
        $totalReferences = count($this->parser->references);
        $record = 23;
        $length = 2 + 6 * $totalReferences;
        $supbook_index = 0;
        $header = pack("vv", $record, $length);
        $data = pack("v", $totalReferences);
        for ($i = 0; $i < $totalReferences; $i++) {
            $data .= $this->parser->references[$i];
        }
        return $this->writeData($header . $data);
    }
    private function writeStyle()
    {
        $record = 659;
        $length = 4;
        $ixfe = 32768;
        $BuiltIn = 0;
        $iLevel = 255;
        $header = pack("vv", $record, $length);
        $data = pack("vCC", $ixfe, $BuiltIn, $iLevel);
        $this->append($header . $data);
    }
    private function writeNumberFormat($format, $ifmt)
    {
        $record = 1054;
        $numberFormatString = PHPExcel_Shared_String::UTF8toBIFF8UnicodeLong($format);
        $length = 2 + strlen($numberFormatString);
        $header = pack("vv", $record, $length);
        $data = pack("v", $ifmt) . $numberFormatString;
        $this->append($header . $data);
    }
    private function writeDateMode()
    {
        $record = 34;
        $length = 2;
        $f1904 = PHPExcel_Shared_Date::getExcelCalendar() == PHPExcel_Shared_Date::CALENDAR_MAC_1904 ? 1 : 0;
        $header = pack("vv", $record, $length);
        $data = pack("v", $f1904);
        $this->append($header . $data);
    }
    private function writeExternalCount($cxals)
    {
        $record = 22;
        $length = 2;
        $header = pack("vv", $record, $length);
        $data = pack("v", $cxals);
        $this->append($header . $data);
    }
    private function writeExternalSheet($sheetname)
    {
        $record = 23;
        $length = 2 + strlen($sheetname);
        $cch = strlen($sheetname);
        $rgch = 3;
        $header = pack("vv", $record, $length);
        $data = pack("CC", $cch, $rgch);
        $this->append($header . $data . $sheetname);
    }
    private function writeNameShort($index, $type, $rowmin, $rowmax, $colmin, $colmax)
    {
        $record = 24;
        $length = 36;
        $grbit = 32;
        $chKey = 0;
        $cch = 1;
        $cce = 21;
        $ixals = $index + 1;
        $itab = $ixals;
        $cchCustMenu = 0;
        $cchDescription = 0;
        $cchHelptopic = 0;
        $cchStatustext = 0;
        $rgch = $type;
        $unknown03 = 59;
        $unknown04 = 65535 - $index;
        $unknown05 = 0;
        $unknown06 = 0;
        $unknown07 = 4231;
        $unknown08 = 32773;
        $header = pack("vv", $record, $length);
        $data = pack("v", $grbit);
        $data .= pack("C", $chKey);
        $data .= pack("C", $cch);
        $data .= pack("v", $cce);
        $data .= pack("v", $ixals);
        $data .= pack("v", $itab);
        $data .= pack("C", $cchCustMenu);
        $data .= pack("C", $cchDescription);
        $data .= pack("C", $cchHelptopic);
        $data .= pack("C", $cchStatustext);
        $data .= pack("C", $rgch);
        $data .= pack("C", $unknown03);
        $data .= pack("v", $unknown04);
        $data .= pack("v", $unknown05);
        $data .= pack("v", $unknown06);
        $data .= pack("v", $unknown07);
        $data .= pack("v", $unknown08);
        $data .= pack("v", $index);
        $data .= pack("v", $index);
        $data .= pack("v", $rowmin);
        $data .= pack("v", $rowmax);
        $data .= pack("C", $colmin);
        $data .= pack("C", $colmax);
        $this->append($header . $data);
    }
    private function writeNameLong($index, $type, $rowmin, $rowmax, $colmin, $colmax)
    {
        $record = 24;
        $length = 61;
        $grbit = 32;
        $chKey = 0;
        $cch = 1;
        $cce = 46;
        $ixals = $index + 1;
        $itab = $ixals;
        $cchCustMenu = 0;
        $cchDescription = 0;
        $cchHelptopic = 0;
        $cchStatustext = 0;
        $rgch = $type;
        $unknown01 = 41;
        $unknown02 = 43;
        $unknown03 = 59;
        $unknown04 = 65535 - $index;
        $unknown05 = 0;
        $unknown06 = 0;
        $unknown07 = 4231;
        $unknown08 = 32776;
        $header = pack("vv", $record, $length);
        $data = pack("v", $grbit);
        $data .= pack("C", $chKey);
        $data .= pack("C", $cch);
        $data .= pack("v", $cce);
        $data .= pack("v", $ixals);
        $data .= pack("v", $itab);
        $data .= pack("C", $cchCustMenu);
        $data .= pack("C", $cchDescription);
        $data .= pack("C", $cchHelptopic);
        $data .= pack("C", $cchStatustext);
        $data .= pack("C", $rgch);
        $data .= pack("C", $unknown01);
        $data .= pack("v", $unknown02);
        $data .= pack("C", $unknown03);
        $data .= pack("v", $unknown04);
        $data .= pack("v", $unknown05);
        $data .= pack("v", $unknown06);
        $data .= pack("v", $unknown07);
        $data .= pack("v", $unknown08);
        $data .= pack("v", $index);
        $data .= pack("v", $index);
        $data .= pack("v", 0);
        $data .= pack("v", 16383);
        $data .= pack("C", $colmin);
        $data .= pack("C", $colmax);
        $data .= pack("C", $unknown03);
        $data .= pack("v", $unknown04);
        $data .= pack("v", $unknown05);
        $data .= pack("v", $unknown06);
        $data .= pack("v", $unknown07);
        $data .= pack("v", $unknown08);
        $data .= pack("v", $index);
        $data .= pack("v", $index);
        $data .= pack("v", $rowmin);
        $data .= pack("v", $rowmax);
        $data .= pack("C", 0);
        $data .= pack("C", 255);
        $data .= pack("C", 16);
        $this->append($header . $data);
    }
    private function writeCountry()
    {
        $record = 140;
        $length = 4;
        $header = pack("vv", $record, $length);
        $data = pack("vv", $this->countryCode, $this->countryCode);
        return $this->writeData($header . $data);
    }
    private function writeRecalcId()
    {
        $record = 449;
        $length = 8;
        $header = pack("vv", $record, $length);
        $data = pack("VV", 449, 124519);
        return $this->writeData($header . $data);
    }
    private function writePalette()
    {
        $aref = $this->palette;
        $record = 146;
        $length = 2 + 4 * count($aref);
        $ccv = count($aref);
        $data = "";
        foreach ($aref as $color) {
            foreach ($color as $byte) {
                $data .= pack("C", $byte);
            }
        }
        $header = pack("vvv", $record, $length, $ccv);
        $this->append($header . $data);
    }
    private function writeSharedStringsTable()
    {
        $continue_limit = 8224;
        $recordDatas = [];
        $recordData = pack("VV", $this->stringTotal, $this->stringUnique);
        foreach (array_keys($this->stringTable) as $string) {
            $headerinfo = unpack("vlength/Cencoding", $string);
            $encoding = $headerinfo["encoding"];
            $finished = false;
            while ($finished === false) {
                if(strlen($recordData) + strlen($string) <= $continue_limit) {
                    $recordData .= $string;
                    if(strlen($recordData) + strlen($string) == $continue_limit) {
                        $recordDatas[] = $recordData;
                        $recordData = "";
                    }
                    $finished = true;
                } else {
                    $space_remaining = $continue_limit - strlen($recordData);
                    $min_space_needed = $encoding == 1 ? 5 : 4;
                    if($space_remaining < $min_space_needed) {
                        $recordDatas[] = $recordData;
                        $recordData = "";
                    } else {
                        $effective_space_remaining = $space_remaining;
                        if($encoding == 1 && (strlen($string) - $space_remaining) % 2 == 1) {
                            --$effective_space_remaining;
                        }
                        $recordData .= substr($string, 0, $effective_space_remaining);
                        $string = substr($string, $effective_space_remaining);
                        $recordDatas[] = $recordData;
                        $recordData = pack("C", $encoding);
                    }
                }
            }
        }
        if(0 < strlen($recordData)) {
            $recordDatas[] = $recordData;
        }
        $chunk = "";
        foreach ($recordDatas as $i => $recordData) {
            $record = $i == 0 ? 252 : 60;
            $header = pack("vv", $record, strlen($recordData));
            $data = $header . $recordData;
            $chunk .= $this->writeData($data);
        }
        return $chunk;
    }
    private function writeMsoDrawingGroup()
    {
        if(isset($this->escher)) {
            $writer = new PHPExcel_Writer_Excel5_Escher($this->escher);
            $data = $writer->close();
            $record = 235;
            $length = strlen($data);
            $header = pack("vv", $record, $length);
            return $this->writeData($header . $data);
        }
        return "";
    }
    public function getEscher()
    {
        return $this->escher;
    }
    public function setEscher(PHPExcel_Shared_Escher $pValue = NULL)
    {
        $this->escher = $pValue;
    }
}

?>